package com.swjd.jingle;

import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;

import com.google.gson.Gson;
import com.hxl.okhttp.response.JsonArrayResponseHandler;
import com.swjd.jingle.pojo.MyType;
import com.swjd.jingle.util.App;
import com.swjd.jingle.util.Constant;
import com.xuexiang.xui.widget.toast.XToast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class InitData {

    public static void initType(FragmentActivity context, App app) {
        Map<String, String> params = new HashMap<>();
        params.put("tName", "");
        app.getOkHttpUtils().post(Constant.BASE_URL + Constant.selectAllTypeOne, params, new JsonArrayResponseHandler() {
            @Override
            public void onSuccess(int statusCode, JSONArray response) {
                try {
                    List<MyType> types = new ArrayList<>();
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject data = (JSONObject) response.get(i);
                        MyType myType = new Gson().fromJson(data.toString(), MyType.class);
                        types.add(myType);
                    }
                    app.setTypes(types);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int statusCode, String error_msg) {
                XToast.error(context, "网络请求失败!", Toast.LENGTH_SHORT).show();
            }
        });
    }


}
